#import "NSDate+CTTool.h"
@implementation NSDate (CTTool)
+ (NSTimeInterval)timeIntervalWithStart:(NSDate *)start end:(NSDate *)end {
    NSTimeInterval time = [end timeIntervalSinceDate:start];
    return time;
}
+ (NSTimeInterval)timeIntervalWithStartInterval:(NSTimeInterval)startInterval endInterval:(NSTimeInterval)endInterval {
    NSDate *date1 = [NSDate dateWithTimeIntervalSince1970:startInterval];
    NSDate *date2 = [NSDate dateWithTimeIntervalSince1970:endInterval];
    NSTimeInterval difference = [date2 timeIntervalSinceDate:date1];
    NSLog(@"timeInterval differ：%f second", difference);
    return difference;
}
#pragma mark - imestamp
+ (NSString *)timestampWithDate:(NSDate *)date {
    NSTimeInterval stamp = [date timeIntervalSince1970];
    return [NSString stringWithFormat:@"%f", stamp];
}
+ (NSDate *)dateWithTimestamp:(NSString *)timeStamp {
    NSTimeInterval stamp = timeStamp.doubleValue;
    return [NSDate dateWithTimeIntervalSince1970:stamp];
}
#pragma mark - NSString->NSDate
+ (NSDate *)dateWithString:(NSString *)string {
    NSDateFormatter *formatter = [self getFormatter];
    NSDate *date = [formatter dateFromString:string];
    return date;
}
+ (NSDate *)dateWithString:(NSString *)string format:(NSString *)format {
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init] ;
    [formatter setDateFormat:format];
    formatter.timeZone = [NSTimeZone localTimeZone];
    NSDate *date = [formatter dateFromString:string];
    return date;
}
#pragma mark - NSDate to NSString
+ (NSString *)stringWithDate:(NSDate *)date {
    NSDateFormatter *formatter = [self getFormatter];
    NSString *dateString = [formatter stringFromDate:date];
    return dateString;
}
+ (NSString *)stringWithDate:(NSDate *)date format:(NSString *)format {
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init] ;
    [formatter setDateFormat:format];
    formatter.timeZone = [NSTimeZone localTimeZone];;
    NSString *dateString = [formatter stringFromDate:date];
    return dateString;
}
+ (NSDateFormatter *)getFormatter {
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init] ;
    [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
    NSTimeZone* timeZone = [NSTimeZone localTimeZone];
    formatter.timeZone = timeZone;
    return formatter;
}
- (NSString *)stringWithFormat:(NSString *)format {
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:format];
    [formatter setLocale:[NSLocale currentLocale]];
    return [formatter stringFromDate:self];
}
- (NSString *)stringWithFormat:(NSString *)format timeZone:(NSTimeZone *)timeZone locale:(NSLocale *)locale {
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:format];
    if (timeZone) [formatter setTimeZone:timeZone];
    if (locale) [formatter setLocale:locale];
    return [formatter stringFromDate:self];
}
+ (BOOL)juncCompareOneDate:(NSDate *)oneDate withAnotherDate:(NSDate *)anotherDate
{
    NSDateFormatter *df = [[NSDateFormatter alloc]init];
    [df setDateFormat:@"dd-MM-yyyy HH:mm:ss"];
    NSString *oneDayStr = [df stringFromDate:oneDate];
    NSString *anotherDayStr = [df stringFromDate:anotherDate];
    NSDate *dateA = [df dateFromString:oneDayStr];
    NSDate *dateB = [df dateFromString:anotherDayStr];
    NSComparisonResult result = [dateA compare:dateB];
    if (result == NSOrderedAscending)
    {  
        return 1;
    }else if (result == NSOrderedDescending)
    {  
        return -1;
    }
    return 0;
}
+ (BOOL)juncCompareOneDateStr:(NSString *)oneDateStr withAnotherDateStr:(NSString *)anotherDateStr
{
    NSDateFormatter *df = [[NSDateFormatter alloc]init];
    [df setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *dateA = [[NSDate alloc]init];
    NSDate *dateB = [[NSDate alloc]init];
    dateA = [df dateFromString:oneDateStr];
    dateB = [df dateFromString:anotherDateStr];
    NSComparisonResult result = [dateA compare:dateB];
    if (result == NSOrderedAscending)
    {  
        return 1;
    }else if (result == NSOrderedDescending)
    {  
        return -1;
    }
    return 0;
}
+ (NSInteger)secondWithOneDateStr:(NSString *)oneDateStr anotherDateStr:(NSString *)anotherDateStr
{
    NSDateFormatter *df = [[NSDateFormatter alloc]init];
    [df setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *dateA = [[NSDate alloc]init];
    NSDate *dateB = [[NSDate alloc]init];
    dateA = [df dateFromString:oneDateStr];
    dateB = [df dateFromString:anotherDateStr];
    NSTimeInterval time = [dateB timeIntervalSinceDate:dateA];
    NSString *timeStr = [NSString stringWithFormat:@"%f",time];
    NSLog(@"time=%f",time);
    NSInteger timeInt = [timeStr integerValue];
    NSLog(@"second%ld",timeInt);
    return timeInt;
}
@end
